﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using System.Reflection;
using SQLCE.Data;
using Autofac.Integration.Mvc;
using SQLCE.Service;
using System.Data.Entity;
using SQLCE.Entity;

namespace SQLCE
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
               "myroute",
               "{controller}/{action}/{grade}-{subject}-{version}",
               new
               {
                   controller = "Home",
                   action = "About",
                   grade = 0,
                   subject = 0,
                   version = 0
               },
               new
               {
                   action = "About|About2",
                   grade = @"^\d+$",
                   subject = @"^\d+$",
                   version = @"^\d+$"
               },
               new string[] { "SQLCE.Controllers" }

           );


            routes.MapRoute(
            "Default", // 路由名称
            "{controller}/{action}/{id}", // 带有参数的 URL
            new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            },
            null,
            new string[] { "SQLCE.Controllers" }
        );



        }

        protected void Application_Start()
        {
            #region 注册页面引擎
            ViewEngines.Engines.Clear();//清空所有FindView規則
            ViewEngines.Engines.Insert(0, new CustomRazorViewEngine());
            #endregion

            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);

            #region Autofac注入
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<LocalTestEntities>().As<DbContext>().InstancePerHttpRequest();
            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();//DATA层注入，但不引入DATA
            builder.RegisterType<ProductManager>().As<ProductManager>().InstancePerHttpRequest(); //从HTTP请求中重到注入点
            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            #endregion

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// 建立一个页面搜索引擎，针对ASPX
        /// 可以改变默认VIEW搜索规则
        /// </summary>
        private class CustomWebFormViewEngine : WebFormViewEngine
        {
            public CustomWebFormViewEngine()
                : base()
            {
                base.AreaMasterLocationFormats = new string[] 
                { 
                    "~/Areas/{2}/Views/{1}/{0}.master",
                    "~/Areas/{2}/Views/Shared/{0}.master", 
                    "~/Views/Shared/{0}.master" 
                };
                base.AreaViewLocationFormats = new string[] 
                { 
                    "~/Areas/{2}/Views/{1}/{0}.aspx", 
                    "~/Areas/{2}/Views/{1}/{0}.ascx", 
                    "~/Areas/{2}/Views/Shared/{0}.aspx", 
                    "~/Areas/{2}/Views/Shared/{0}.ascx", 
                    "~/Views/Shared/{0}.ascx" 
                };
                base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats;

            }
        }
        /// <summary>
        /// 建立一个页面搜索引擎，针对Razor
        /// 按著自己希望的VIEW定位順序去訂定，默認情況下areas区域的view查询时，先找自己的areas，然后找view/shared，
        /// 我们可以改变它的顺序
        /// </summary>
        private class CustomRazorViewEngine : RazorViewEngine
        {
            public CustomRazorViewEngine()
                : base()
            {
                base.AreaMasterLocationFormats = new string[]
                {
                    "~/Views/Shared/{0}.cshtml",
                    "~/Areas/{2}/Views/{1}/{0}.cshtml", 
                    "~/Areas/{2}/Views/Shared/{0}.cshtml", 
                  };

                base.AreaViewLocationFormats = new string[] 
                { 
                     "~/Areas/{2}/Views/{1}/{0}.cshtml", 
                     "~/Areas/{2}/Views/Shared/{0}.cshtml",
                     "~/Views/Shared/{0}.cshtml"
                };

                base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats;
            }
        }
    }

}